<?php
require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\RequestException;;

use GuzzleHttp\Promise;

$GLOBALS['appID'] = "219AAA33E4D569E637426A7115CC8DAA";
$GLOBALS['appKey'] = '45661418513867396';
$GLOBALS['url'] = 'https://area78-win.pospal.cn:443/';

//遍历
function get_post($url, $data, $headers)
{
    $client = new Client();
    try {
        $response = $client->post($GLOBALS['url'] . $url, [
            'headers' => $headers,
            'json' => $data
        ]);
        // 输出请求结果
        return $response->getBody();
    } catch (RequestException $e) {
        // 输出请求异常
        return $e->getMessage();
    }
}

$phone = $_POST['phone'];
$store = $_POST['store'];
$startTime = $_POST['startTime'];
$endTime = $_POST['endTime'];

$variable = [
    'Atelier001' => [
        'store' => 'Atelier001',
        'url' => 'pospal-api2/openapi/v1/customerOpenapi/queryBytel',
        'appkey' => '45661418513867396',
        'appid' => '219AAA33E4D569E637426A7115CC8DAA',
        'body' => [
            "appId" => '219AAA33E4D569E637426A7115CC8DAA',
            "customerTel" => $phone,
        ],
        'headers' => [
            'User-Agent' => 'openApi',
            'Content-Type' => 'application/json',
            'data-signature' => ''
        ]
    ],
    'AtelierJFB' => [
        'store' => 'AtelierJFB',
        'url' => 'pospal-api2/openapi/v1/customerOpenapi/queryBytel',
        'appkey' => '860865420322249048',
        'appid' => 'D03D1586EE6DF9540A1B7B86D8C229FA',
        'body' => [
            "appId" => 'D03D1586EE6DF9540A1B7B86D8C229FA',
            "customerTel" => $phone,
        ],
        'headers' => [
            'User-Agent' => 'openApi',
            'Content-Type' => 'application/json',
            'data-signature' => ''
        ]
    ],
    'JYJFB888888' =>  [
        'store' => 'JYJFB888888',
        'url' => 'pospal-api2/openapi/v1/customerOpenapi/queryBytel',
        'appkey' => '57896692810169407',
        'appid' => 'E551304FF510CDD9B5173B9AB0917BEE',
        'body' => [
            "appId" => 'E551304FF510CDD9B5173B9AB0917BEE',
            "customerTel" => $phone,
        ],
        'headers' => [
            'User-Agent' => 'openApi',
            'Content-Type' => 'application/json',
            'data-signature' => ''
        ]
    ]
];
if (!empty($store) && empty($variable[$store])) {
    echo json_encode([
        'code' => 1,
        'data' => '没有分店'
    ]);
    exit;
}

if (!empty($store) && !empty($variable[$store])) {

    $variable[$store]['headers']['data-signature'] = strtoupper(md5($variable[$store]['appkey'] . json_encode($variable[$store]['body'])));
    $res = json_decode(get_post($variable[$store]['url'], $variable[$store]['body'], $variable[$store]['headers']));
    if (empty($res->data[0]->customerUid)) {
        echo json_encode([
            'code' => 1,
            'data' => '无此会员'
        ]);
        exit;
    }
    $body = [
        "appId" => $variable[$store]['body']['appId'],
        "startTime" => $startTime,
        "endTime" => $endTime,
        "customerUid" => $res->data[0]->customerUid,
        "queryAllStoreConsume" => 1,
        // "postBackParameter" => [
        //     "parameterType" => "LAST_RESULT_MIN_ID",
        //     "parameterValue" => 31465712
        // ]
    ];
    $variable[$store]['headers']['data-signature'] = strtoupper(md5($variable[$store]['appkey'] . json_encode($body)));
    $order = json_decode(get_post('pospal-api2/openapi/v1/ticketOpenApi/queryCustomerHistoryTicketsByUid', $body, $variable[$store]['headers']))->data->result;


    $res->data[0]->order = $order ?? [];
    $resdata = $res;
} else {
    foreach ($variable as $key => $value) {
        $value['headers']['data-signature'] = strtoupper(md5($value['appkey'] . json_encode($value['body'])));

        $res = json_decode(get_post($value['url'], $value['body'], $value['headers']));

        if ($res->status == 'success' && !empty($res->data)) {

            $val[$value['appid']] = $res->data;

            //订单
            $body = [
                "appId" => $value['body']['appId'],
                "startTime" => $startTime,
                "endTime" => $endTime,
                // 'orderNo' => '202308161407542120001'
                "customerUid" => $res->data[0]->customerUid,
                "queryAllStoreConsume" => 1,
                // 'id' => $res->data[0]->extInfo->id
            ];
            $value['headers']['data-signature'] = strtoupper(md5($value['appkey'] . json_encode($body)));
            $res->data[0]->order = json_decode(get_post('pospal-api2/openapi/v1/ticketOpenApi/queryCustomerHistoryTicketsByUid', $body, $value['headers']));
        } else {
            $val[$value['appid']] = [];
        }
        $resdata = $val;
    }
}

echo json_encode([
    'code' => 1,
    'data' => $resdata
]);


// $body = [
//     "appId" => $GLOBALS['appID'],
//     "customerTel" => 13681917876,
// ];
// $headers = [
//     'User-Agent' => 'openApi',
//     'Content-Type' => 'application/json',
//     'data-signature' => strtoupper(md5($GLOBALS['appKey'] . json_encode($body)))
// ];

//异步
// $client = new Client(['base_uri' => 'https://area78-win.pospal.cn/']);

// $promises = [
//     'Atelier001' => $client->getAsync('/pospal-api2/openapi/v1/customerOpenapi/queryBytel', [
//         'headers' => [
//             'User-Agent' => 'openApi',
//             'Content-Type' => 'application/json',
//             'data-signature' => strtoupper(md5('45661418513867396' . json_encode([
//                 "appId" => '219AAA33E4D569E637426A7115CC8DAA',
//                 "customerTel" => 13681917876,
//             ])))
//         ],
//         'json' => [
//             "appId" => '219AAA33E4D569E637426A7115CC8DAA',
//             "customerTel" => 13681917876,
//         ]
//     ]),
//     'AtelierJFB' => $client->getAsync('/pospal-api2/openapi/v1/customerOpenapi/queryBytel', [
//         'headers' => [
//             'User-Agent' => 'openApi',
//             'Content-Type' => 'application/json',
//             'data-signature' => strtoupper(md5('860865420322249048' . json_encode([
//                 "appId" => 'D03D1586EE6DF9540A1B7B86D8C229FA',
//                 "customerTel" => 13681917876,
//             ])))
//         ],
//         'json' => [
//             "appId" => 'D03D1586EE6DF9540A1B7B86D8C229FA',
//             "customerTel" => 13681917876,
//         ]
//     ]),
//     'JYJFB888888' => $client->getAsync('/pospal-api2/openapi/v1/customerOpenapi/queryBytel', [
//         'headers' => [
//             'User-Agent' => 'openApi',
//             'Content-Type' => 'application/json',
//             'data-signature' => strtoupper(md5('57896692810169407' . json_encode([
//                 "appId" => 'E551304FF510CDD9B5173B9AB0917BEE',
//                 "customerTel" => 13681917876,
//             ])))
//         ],
//         'json' => [
//             "appId" => 'E551304FF510CDD9B5173B9AB0917BEE',
//             "customerTel" => 13681917876,
//         ]
//     ]),
// ];
// $responses = Promise\Utils::unwrap($promises);

// $data['Atelier001'] = json_decode($responses['Atelier001']->getBody());
// $data['AtelierJFB'] = json_decode($responses['AtelierJFB']->getBody());
// $data['JYJFB888888'] = json_decode($responses['JYJFB888888']->getBody());
// echo json_encode($data);
// foreach ($variable as $key => $value) {
//     # code...
// }
// //单个请求
// $client = new Client();
// // 设置post请求的参数
// $data = array(
//     "appId" => $GLOBALS['appID'],
//     "customerTel" => $phone,
//     "groupShare"=>1
//     // "postBackParameter" => [
//     //     "parameterType" => "abcd",
//     //     "parameterValue" => ""
//     // ]
// );
// // 设置请求头部
// $headers = array(
//     'User-Agent' => 'openApi',
//     'Content-Type' => 'application/json',
//     'Accept' => 'application/json',
//     'data-signature' => strtoupper(md5($GLOBALS['appKey'] . json_encode($data)))
// );
// // 发送post json请求
// try {
//     $response = $client->post('https://area78-win.pospal.cn/pospal-api2/openapi/v1/customerOpenapi/queryBytel', [
//         'headers' => $headers,
//         'json' => $data
//     ]);
//     // 输出请求结果
//     echo $response->getBody();
// } catch (RequestException $e) {
//     // 输出请求异常
//     echo $e->getMessage();
// }
// function getUnixTimestamp()
// {
//     list($s1, $s2) = explode(' ', microtime());
//     return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
// }

// var_dump(getpost());
// echo getpost($url . 'pospal-api2/openapi/v1/customerOpenapi/queryBytel', [
//     "appId" => $GLOBALS['appID'],
//     "customerTel" => 13681917876,
//     // "postBackParameter"=> [
//     //     "parameterType"=> "",
//     //     "parameterValue"=> "JYJFB888888"
//     // ]
// ]);

// $signature = strtoupper(md5($appKey . $jsonData));
